package conMgr.ui;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.text.ParseException;

import javax.swing.AbstractAction;
import conMgr.model.Conference;
import conMgr.prog.Program;
import conMgr.ui.bases.JDialogEx;
import conMgr.ui.util.ButtonBar;
import conMgr.ui.util.Ui;

@SuppressWarnings("serial")
public class NewConferenceView extends JDialogEx{
	
	
	/**
	 * The conference info view for this panel.
	 */
	private final NewConferenceInfoView infoView; 
	
	/**
	 * The conference 
	 */
	private final Conference conference;

	/**
	 * The button bar for this panel.
	 */
	private final ButtonBar buttonBar;
	
	/**
	 * Constructor for the new conference view
	 * @param conf is the new conference
	 * @throws ParseException
	 */
	public NewConferenceView(final Conference conf) throws ParseException
	{
		setLayout(new GridBagLayout());
		conference = conf;
		infoView = new NewConferenceInfoView(conference);

		GridBagConstraints constraints = new GridBagConstraints();
		
		constraints.insets = Ui.DEFAULT_INSETS;
		constraints.gridx = 0;
		constraints.gridy = 0;
		constraints.anchor = GridBagConstraints.NORTH;
		add(infoView, constraints);
		
		constraints = new GridBagConstraints();
		constraints.insets = Ui.DEFAULT_INSETS;
		constraints.gridx = 0;
		constraints.gridy = 1;
		constraints.gridwidth = 3;
		constraints.anchor = GridBagConstraints.EAST;
		
		buttonBar = new ButtonBar();
		if (conference == null)
			buttonBar.addButton("Create", new OkAction());
		else 
			buttonBar.addButton("Modify", new ModifyAction());
		buttonBar.addButton("Cancel", new CancelAction());
		add(buttonBar, constraints);
		
		finishSetup();
	}

	/**
	 * Provides an action for the Modify button. 
	 */
	class ModifyAction extends AbstractAction
	{

		/**
		 * Handles the action. Updates the given conference and closes the
		 * window.
		 */
		@Override
		public void actionPerformed(final ActionEvent arg0)
		{
			try {
				infoView.modifyConference();
			} catch (ParseException e) {
				e.printStackTrace();
			}
			
			Program.getDataStore().saveAll();
			NewConferenceView.this.close();
		}
	}
	
	/**
	 * Provides an action for the OK button. 
	 */
	class OkAction extends AbstractAction
	{
		/**
		 * The serialization ID of this class.
		 */
		private static final long serialVersionUID = 1341664181816147314L;

		/**
		 * Handles the action. Creates or updates the given paper and closes the
		 * window.
		 */
		@Override
		public void actionPerformed(final ActionEvent arg0)
		{
			
				Conference conf = null;
				try {
					conf = infoView.createConference();
				} catch (ParseException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
				if (conf == null)
					return;
			
			
			Program.getDataStore().saveAll();
			NewConferenceView.this.close();
		}
	}
	
	/**
	 * Provides an action for the Cancel button.
	 */
	class CancelAction extends AbstractAction
	{
		/**
		 * The serialization ID of this class.
		 */
		private static final long serialVersionUID = 2235460547541514775L;

		/**
		 * Handles the action. Closes the window.
		 */
		@Override
		public void actionPerformed(final ActionEvent arg0)
		{
			NewConferenceView.this.close();
		}
	}

}
